/*
 * mi_motor_drv.h
 *
 *  Created on: Dec 10, 2024
 *      Author: Jinming
 */

#ifndef INC_MI_MOTOR_DRV_H_
#define INC_MI_MOTOR_DRV_H_

#include "main.h"
#include "can.h"
#include "cmsis_os.h"

typedef enum {
	MI_MOTOR_MODE_RUN_CTRL = 0,				//小米电机的运控模式
	MI_MOTOR_MODE_POS = 1,					// 位置模式
	MI_MOTOR_MODE_RPM = 2,					// 转速模式
	MI_MOTOR_MODE_TORQUE = 3,				// 转矩模式
	MI_MOTOR_MODE_IDLE,						// 停止转动，与上层兼容
}MI_MOTOR_MODE_e;							//小米电机控制模式

enum
{
	MI_MOTOR_PARAM_RUM_MODE 	= 0x7005,		//run_mode
	MI_MOTOR_PARAM_IQ_REF 		= 0x7006,		//电流模式Iq指令
	MI_MOTOR_PARAM_SPD_REF 		= 0x700A,		//转速模式转速指令
	MI_MOTOR_PARAM_LIMIT_TORQUE = 0x700B,		//转矩限制
	MI_MOTOR_PARAM_CUR_KP 		= 0x7010,		//电流的Kp,默认值0.125
	MI_MOTOR_PARAM_CUR_KI 		= 0x7011,		//电流的Ki,默认值0.0158
	MI_MOTOR_PARAM_CUR_FILT_GAIN = 0x7014,		//电流滤波系数(0~1.0),默认值0.1
	MI_MOTOR_PARAM_LOC_REF 		= 0x7016,		//位置模式角度指令,rad
	MI_MOTOR_PARAM_LIMIT_SPD 	= 0x7017,		//位置模式速度限制,0~30rad/s
	MI_MOTOR_PARAM_LIMIT_CUR 	= 0x7018,		//速度位置模式电流限制,0~23A
	MI_MOTOR_PARAM_MECH_POS 	= 0x7019,		//负载端计圈机械角度,rad
	MI_MOTOR_PARAM_IQF 			= 0x701A,		//iq滤波值,-23~23A
	MI_MOTOR_PARAM_MECH_VEL 	= 0x701B,		//负载端转速,-30~30rad/s
	MI_MOTOR_PARAM_VBUS 		= 0x701C,		//母线电压,V
	MI_MOTOR_PARAM_ROTATION 	= 0x701D,		//圈数
	MI_MOTOR_PARAM_LOC_KP 		= 0x701E,		//位置的kp,默认值40
	MI_MOTOR_PARAM_SPD_KP 		= 0x701F,		//速度的kp,默认值1
	MI_MOTOR_PARAM_SPD_KI 		= 0x7020,		//速度的ki,默认值0.002
};

typedef struct
{
    float ratedTorque;          //额定力矩, Nm
    float maxTorque;			//最大力矩, Nm
    float ratedCurrent;			//额定电流, A
    float maxCurrent;			//最大电流, A
    float maxSpeed;             //最大转速, RPM
    int encoderResolution;      //编码器分辨率, 单圈脉冲数量
    float reductionRatio;
    float maxAcc;               //最大加速度,RPM/s
    float maxDec;               //最大减速度,RPM/s
    float stopDec;              //快停减速度,RPM/s
    float tffAccDec;            //转矩变化率,tff/s
    float torqueCoeff;			//转矩常数,Nm/A
}MiMotorAttr_t;           //电机特性

typedef struct
{
//    CANConfig can;
	MI_MOTOR_MODE_e curRunMode;			//当前控制模式
    uint32_t canId;
    MiMotorAttr_t tMotorAttr;

    osMutexId_t mutex;

    uint16_t errCode;
    float pos;					//rad
    float speed;				//rad/s
    float torque;				//Nm
    int16_t temp;					//摄氏度

    //以下为读取的参数
//    float iqRef;				//电流模式Iq,A
//    float spdRef;				//转速模式转速,rad/s
//    float limitTorque;			//转矩限制,Nm
//    float curKp;				//电流的Kp
//    float curKi;				//电流的Ki
//    float curFiltGain;			//电流滤波系数
//    float locRef;				//位置模式角度,rad
//    float limitSpd;				//位置模式速度限制,rad/s
//    float limitCur;				//速度位置模式电流限制,A
//    float mechPos;				//负载端计圈机械角度,rad
//    float iqf;					//iq滤波值,A
//    float mechVel;				//负载端转速,rad/s
//    float vbus;					//母线电压,V
//    float locKp;				//位置的Kp
//    float spdKp;				//速度的Kp
//    float spdKi;				//速度的ki
//    int16_t rotation;			//圈数


    int (*send)(uint32_t, uint8_t *, uint8_t);
    int (*receive)(CANMsgRx_t *, uint32_t);
    void (*clear)(void);			//清除接收缓冲区内容
}MiMotorUnit_t;

int mi_query_motor_dev_id(MiMotorUnit_t *motor, uint8_t *id);
int mi_motor_enable(MiMotorUnit_t *motor);
int mi_motor_disenable(MiMotorUnit_t *motor);
int mi_motor_zero_position_set(MiMotorUnit_t *motor);
//int mi_motor_read_param(MiMotorUnit_t *motor, uint16_t index, void *output);
//int mi_motor_write_param(MiMotorUnit_t *motor, uint16_t index, void *input);

int mi_motor_init(MiMotorUnit_t *motor, uint32_t id, MiMotorAttr_t *attr);
int mi_motor_mode_change(MiMotorUnit_t *motor, uint16_t mode);
MI_MOTOR_MODE_e mi_motor_curMode_get(MiMotorUnit_t *motor);
int mi_motor_torque_set(MiMotorUnit_t *motor, float Nm);
int mi_motor_current_get(MiMotorUnit_t *motor, float *ampere);
int mi_motor_speed_set(MiMotorUnit_t *motor, float rpm);
int mi_motor_speed_get(MiMotorUnit_t *motor, float *speed);
int mi_motor_position_set(MiMotorUnit_t *motor, float deg);
int mi_motor_position_get(MiMotorUnit_t *motor, float *pos);
int mi_motor_current_kp_get(MiMotorUnit_t *motor, float *kp);
int mi_motor_temperature_get(MiMotorUnit_t *motor, int16_t *temp);
int mi_motor_limit_torque_set(MiMotorUnit_t *motor, float torque);
int mi_motor_runctrl_param_set(MiMotorUnit_t *motor, float pos, float speed, float kp, float ki);

#endif /* INC_MI_MOTOR_DRV_H_ */
